<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreateInStock" preIcon="ant-design:plus-outlined"
          >入库</a-button
        >
        <a-button type="primary" @click="handleCreateOutStock" preIcon="ant-design:minus-outlined"
          >出库</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'imageUrl'">
          <Avatar :size="60" :src="record.imageUrl" />
        </template>
        <template v-if="column.key === 'status'">
          <Tag v-if="record.status" :color="record.status === '1' ? 'processing' : 'error'">
            {{ formatDictValue(sysStatusOptions, record.status) }}
          </Tag>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                // auth: ['system:sysRole:updateSysRole'],
                icon: 'ant-design:vertical-align-bottom-outlined',
                tooltip: '编辑入库',
                onClick: handleEditInStock.bind(null, record),
              },
              {
                // auth: ['system:sysRole:removeSysRole'],
                icon: 'ant-design:vertical-align-top-outlined',
                tooltip: '编辑出库',
                onClick: handleEditOutStock.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <InStockDrawer @register="registerInStockDrawer" @success="handleSuccess" />
    <OutStockDrawer @register="registerOutStockDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, onBeforeMount } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getProductStockWithPageApi } from '/@/api/modules/business/product/stock';
  import { useDrawer } from '/@/components/Drawer';
  import InStockDrawer from './InStockDrawer.vue';
  import OutStockDrawer from './OutStockDrawer.vue';
  import { columns, searchFormSchema } from './stock.data';
  import { getDictEntryWithListByDictCodeApi } from '/@/api/modules/system/dictEntry';
  import { formatDictValue } from '/@/utils';
  import { Tag, Avatar } from 'ant-design-vue';
  export default defineComponent({
    name: 'StockManagement',
    components: { BasicTable, InStockDrawer, TableAction, OutStockDrawer, Tag, Avatar },
    setup() {
      const [registerInStockDrawer, { openDrawer: openInStockDrawer }] = useDrawer();
      const [registerOutStockDrawer, { openDrawer: openOutStockDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '物资库存管理',
        api: getProductStockWithPageApi,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
          resetButtonOptions: {
            preIcon: 'ant-design:delete-outlined',
          },
          submitButtonOptions: {
            preIcon: 'ant-design:search-outlined',
          },
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          // auth: ['system:sysRole:updateSysRole', 'system:sysRole:removeSysRole'],
          width: 80,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });

      const sysStatusOptions = ref<any>([]);
      onBeforeMount(async () => {
        sysStatusOptions.value = await getDictEntryWithListByDictCodeApi({
          dictCode: 'sys_status',
        });
      });

      function handleCreateInStock() {
        openInStockDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEditInStock(record: Recordable) {
        openInStockDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleCreateOutStock() {
        openOutStockDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEditOutStock(record: Recordable) {
        openOutStockDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerInStockDrawer,
        registerOutStockDrawer,
        handleCreateInStock,
        handleCreateOutStock,
        handleEditInStock,
        handleEditOutStock,
        handleSuccess,
        formatDictValue,
        sysStatusOptions,
      };
    },
  });
</script>
